package com.icinfo.cloud.provider.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 描述: MD5加密工具类
 *
 * @author YangShangHang
 * @date 2021/10/27
 */
public class Md5Util {
    private static final String[] HEX_DIGITS = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public Md5Util() {
    }

    /**
     * md5加密
     *
     * @param str 待加密字符串
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String md5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] byteArray = str.getBytes("UTF-8");
        return md5(byteArray);
    }

    /**
     * md5加密
     *
     * @param bytes
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String md5(byte[] bytes) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] md5Bytes = md5.digest(bytes);
        StringBuffer sb = new StringBuffer();
        byte[] var4 = md5Bytes;
        int var5 = md5Bytes.length;
        for (int var6 = 0; var6 < var5; ++var6) {
            byte b = var4[var6];
            sb.append(HEX_DIGITS[b >>> 4 & 15]);
            sb.append(HEX_DIGITS[b & 15]);
        }
        return sb.toString();
    }
}
